﻿Imports System.Net
Imports TNCBAL
Imports TNCDAL
Imports HTLibrary
Imports System.Xml

Public Class ucContFee
    Dim db As New TanNamChinhDataContext

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        DataGridViewHelper.DataGridView_InitEvent(dtgListCont)
    End Sub
    Private Sub ucContFee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Panel1.Visible = False
    End Sub

    Public Sub KhoiTaodtgListCont(ByVal chk As Boolean, ByVal FromNo As Integer, ByVal ToNo As Integer, Optional ByVal ID_Cont As Integer = 0)
        Dim dt As DataTable = GetContFee(chk, FromNo, ToNo)
        Dim source As New BindingSource
        source.DataSource = dt
        BindingNavigator1.BindingSource = source


        dtgListCont.DataSource = source
        FormatdtgListCont()
        Dim index As Integer = 0
        If ID_Cont <> 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
                If dtgListCont.Rows(i).Cells("ID_Cont").Value = ID_Cont Then
                    index = i
                    Exit For
                End If
            Next
            dtgListCont.Rows(index).Cells("ContNo").Selected = True
        End If
    End Sub

    Public Sub FormatdtgListCont()

        dtgListCont.RowHeadersWidth = 20
        dtgListCont.RowHeadersDefaultCellStyle.ForeColor = Color.Blue
        dtgListCont.RowTemplate.Height = 25

        dtgListCont.MultiSelect = False
        dtgListCont.ReadOnly = True
        'dtgListCont.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgListCont.BackgroundColor = Color.White
        dtgListCont.AllowUserToAddRows = False
        dtgListCont.AllowUserToDeleteRows = False
        dtgListCont.AllowUserToResizeColumns = False
        dtgListCont.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgListCont.Columns("ID_Cont").Visible = False
        With dtgListCont.Columns("FileNo")
            .Width = 80
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        End With
        With dtgListCont.Columns("ContNo")
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        End With

        For i As Integer = 3 To dtgListCont.ColumnCount - 1
            With dtgListCont.Columns(i)
                .DefaultCellStyle.Format = "#,###.##"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .Width = 80
            End With
        Next

        Dim f As New Font(Me.Font, FontStyle.Bold)

        With dtgListCont.Columns("TOTAL")
            .DefaultCellStyle.BackColor = Color.Azure
            .DefaultCellStyle.Font = f
        End With
    End Sub

    Public Function GetContFee(ByVal IsExport As Boolean, ByVal FromNo As Integer, ByVal ToNo As Integer) As DataTable
        Dim listFee = From p In db.tblFees
                Where (p.ID_FeeType = 3 And p.IsActive = True)
                Select p
                Order By p.Rank


        Dim listCont = From p In db.tblConts
                       Where p.tblHBL.tblFileMBL.tblFile.IsExport = IsExport And p.tblHBL.tblFileMBL.tblFile.FileNo >= FromNo And p.tblHBL.tblFileMBL.tblFile.FileNo <= ToNo
                       Order By p.tblHBL.tblFileMBL.tblFile.FileNo >= FromNo And p.tblHBL.tblFileMBL.tblFile.FileNo
                       Select p.ID_Cont,
                       FileNo = IIf(IsExport, p.tblHBL.tblFileMBL.tblFile.FileNo.ToString + "/EX", p.tblHBL.tblFileMBL.tblFile.FileNo.ToString + "/IM"),
                       p.ContNo


        Dim dt As New DataTable
        dt.Clear()
        'Row la loai container
        'Col la cac loai phi cua hang tau theo tung loai cont

        Dim column As DataColumn

        column = New DataColumn()
        column.DataType = System.Type.GetType("System.Int32")
        column.ColumnName = "ID_Cont"
        dt.Columns.Add(column)

        column = New DataColumn()
        column.DataType = System.Type.GetType("System.String")
        column.ColumnName = "FileNo"
        dt.Columns.Add(column)

        column = New DataColumn()
        column.DataType = System.Type.GetType("System.String")
        column.ColumnName = "ContNo"
        dt.Columns.Add(column)

        For Each f As tblFee In listFee
            Dim feeName As String
            Dim ID_Fee As Integer

            feeName = f.FeeName
            ID_Fee = f.ID_Fee

            column = New DataColumn()
            column.DataType = System.Type.GetType("System.Double")
            column.ColumnName = feeName
            column.Caption = ID_Fee

            dt.Columns.Add(column)
        Next

        column = New DataColumn()
        column.DataType = System.Type.GetType("System.Double")
        column.ColumnName = "TOTAL"
        dt.Columns.Add(column)

        Dim dc As New TanNamChinhDataContext

        For Each cont In listCont
            Dim row As DataRow
            row = dt.NewRow
            row(0) = cont.ID_Cont
            row(1) = cont.FileNo
            row(2) = cont.ContNo

            Dim contfee = From p In dc.tblContFees
                          Where p.ID_Cont = cont.ID_Cont And p.tblFee.IsActive = True
                          Order By p.tblFee.Rank
                          Select p

            If contfee Is Nothing Then
                For i As Integer = 3 To dt.Columns.Count - 1
                    row(i) = 0
                Next
            Else
                Dim i As Integer = 0
                Dim total As Double = 0
                For Each fee In contfee
                    row(3 + i) = fee.Price
                    total += fee.Price
                    i += 1
                Next
                row(3 + i) = total
            End If
            dt.Rows.Add(row)
        Next

        Return dt
    End Function

    Public Function GetPrice(ByVal ID_Cont As Integer, ByVal ID_Fee As Integer) As Double
        Dim kq As Double = 0
        Try
            Dim ft = (From p In db.tblContFees
                    Where p.ID_Cont = ID_Cont And p.ID_Fee = ID_Fee
                    Select p).Single
            kq = ft.Price
        Catch ex As Exception

        End Try

        Return kq
    End Function

#Region "Detail Fee"
    '    Public Sub KhoiTaodtgContFee(ByVal ID_Cont As Integer)
    '        'Kiem tra ton tai trong tblContFee chưa?
    '        'Neu chua Insert các loại phí Transport vào tblContFee
    '        Dim c_contfee = (From p In db.tblContFees
    '                         Where p.ID_Cont = ID_Cont
    '                         Select p).Count
    '        If c_contfee = 0 Then
    '            Dim fee_transport = From p In db.tblFees
    '                                Where p.ID_FeeType = 3
    '                                Select p

    '            For Each fee As tblFee In fee_transport
    '                Dim cont_fee As New tblContFee
    '                cont_fee.ID_Cont = ID_Cont
    '                cont_fee.ID_Fee = fee.ID_Fee
    '                cont_fee.Price = 0
    '                cont_fee.UnitPrice = 0
    '                cont_fee.Quantity = 1
    '                cont_fee.ID_Currency = 1 'VND
    '                db.tblContFees.InsertOnSubmit(cont_fee)
    '                db.SubmitChanges()
    '            Next

    '        End If

    '        Dim contFee = From p In db.tblContFees
    '                     Where p.ID_Cont = ID_Cont
    '                     Select New With {p.ID_ContFee, p.ID_Fee, p.ID_Payee, p.tblFee.FeeName, p.UnitPrice, p.Quantity, p.Price, p.InvoiceNo, p.Description, p.ID_Currency}

    '        dtgListCont.DataSource = contFee

    '        FormatdtgContFee()
    '        'Tinh TongTien theo Cont
    '        Dim TongTien As Double = 0

    '        For i As Integer = 0 To dtgListCont.RowCount - 1
    '            TongTien += dtgListCont.Rows(i).Cells("Price").Value
    '        Next
    '        lblTotal.Text = TongTien.ToString("#,###")
    '    End Sub

    '    Public Sub FormatdtgContFee()
    '        dtgListCont.RowHeadersWidth = 20
    '        dtgListCont.RowHeadersDefaultCellStyle.ForeColor = Color.Blue
    '        'dtgContFee.RowTemplate.Height = 25

    '        dtgListCont.MultiSelect = False
    '        dtgListCont.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    '        'dtgContFee.ReadOnly = True
    '        dtgListCont.BackgroundColor = Color.White
    '        dtgListCont.AllowUserToAddRows = False
    '        dtgListCont.AllowUserToDeleteRows = False
    '        dtgListCont.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells


    '        dtgListCont.Columns("ID_ContFee").Visible = False
    '        dtgListCont.Columns("ID_Fee").Visible = False
    '        dtgListCont.Columns("ID_Payee").Visible = False
    '        dtgListCont.Columns("ID_Currency").Visible = False

    '        'dtgContFee.Columns("ID_Cont").Visible = False
    '        'dtgContFee.Columns("ID_AdvanceVoucher").Visible = False
    '        'dtgContFee.Columns("ID_ReceiptVoucher").Visible = False
    '        'dtgContFee.Columns("ID_PaymentVoucher").Visible = False
    '        'dtgContFee.Columns("DriverName").Visible = False
    '        'dtgContFee.Columns("ID_FeeType").Visible = False
    '        'dtgContFee.Columns("FeeAbbreviated").Visible = False
    '        'dtgContFee.Columns("PhoneNo").Visible = False
    '        'dtgContFee.Columns("Address").Visible = False
    '        'dtgContFee.Columns("ID_Manager").Visible = False

    '        With dtgListCont.Columns("Price")
    '            .HeaderText = "Thành tiền"
    '            .Width = 100
    '            .DefaultCellStyle.Format = "#,###"
    '            .ReadOnly = True
    '            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    '        End With
    '        With dtgListCont.Columns("UnitPrice")
    '            .HeaderText = "Đơn giá"
    '            .Width = 100
    '            .DefaultCellStyle.Format = "#,###"
    '            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    '        End With
    '        With dtgListCont.Columns("Quantity")
    '            .HeaderText = "Số lượng"
    '            .Width = 100
    '            .DefaultCellStyle.Format = "#,###"
    '            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    '        End With
    '        With dtgListCont.Columns("InvoiceNo")
    '            .HeaderText = "Số hóa đơn"
    '            .Width = 100
    '        End With
    '        With dtgListCont.Columns("Description")
    '            .HeaderText = "Ghi chú"
    '            .Width = 100
    '        End With
    '        With dtgListCont.Columns("FeeName")
    '            .HeaderText = "Loại phí"
    '            .Width = 150
    '            .ReadOnly = True
    '        End With
    '    End Sub

    '    Private Sub AddComboBoxColumns()
    '        Dim comboboxColumn As DataGridViewComboBoxColumn
    '        comboboxColumn = CreateComboBoxColumn()
    '        SetAlternateChoicesUsingDataSource(comboboxColumn)
    '        comboboxColumn.HeaderText = "Người đóng tiền"
    '        dtgListCont.Columns.Add(comboboxColumn)

    '        'comboboxColumn = CreateComboBoxColumn()
    '        'SetAlternateChoicesUsingItems(comboboxColumn)
    '        'comboboxColumn.HeaderText = _
    '        '    "TitleOfCourtesy (via Items property)"
    '        '' Tack this example column onto the end.
    '        'DataGridView1.Columns.Add(comboboxColumn)
    '    End Sub

    '    Private Function CreateComboBoxColumn() As DataGridViewComboBoxColumn
    '        Dim column As New DataGridViewComboBoxColumn()

    '        With column
    '            .DataPropertyName = "ID_Payee"
    '            .HeaderText = "Người đóng tiền"
    '            .DropDownWidth = 160
    '            .Width = 120
    '            .MaxDropDownItems = 3
    '            .FlatStyle = FlatStyle.Flat
    '            .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
    '        End With
    '        Return column
    '    End Function

    '    Private Sub SetAlternateChoicesUsingDataSource(ByVal comboboxColumn As DataGridViewComboBoxColumn)
    '        Dim driver = From p In db.tblDrivers
    '                       Select p
    '        With comboboxColumn
    '            .DataSource = driver
    '            .ValueMember = "ID_Driver"
    '            .DisplayMember = "DriverName"
    '        End With
    '    End Sub

    '    Public Function TaoColumscbb(ByVal colName As String) As DataGridViewComboBoxColumn
    '        Dim driver = From p In db.tblDrivers
    '                       Select p
    '        Dim cbbDriver As New DataGridViewComboBoxColumn
    '        cbbDriver.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
    '        cbbDriver.Name = colName
    '        cbbDriver.HeaderText = "Người đóng phí"
    '        cbbDriver.DataSource = driver
    '        cbbDriver.DisplayMember = "DriverName"
    '        cbbDriver.ValueMember = "ID_Driver"
    '        Return cbbDriver
    '    End Function

    '    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    '        'Update tblConFee
    '        Dim TongTien As Double = 0

    '        For i As Integer = 0 To dtgListCont.RowCount - 1
    '            TongTien += dtgListCont.Rows(i).Cells("Price").Value
    '            Dim ID_ContFee As Integer
    '            ID_ContFee = dtgListCont.Rows(i).Cells("ID_ContFee").Value

    '            Dim contfee = (From p In db.tblContFees
    '                       Where p.ID_ContFee = ID_ContFee
    '                       Select p).Single

    '            contfee.ID_Payee = dtgListCont.Rows(i).Cells("ID_Payee").Value
    '            contfee.InvoiceNo = dtgListCont.Rows(i).Cells("InvoiceNo").Value
    '            contfee.Price = dtgListCont.Rows(i).Cells("Price").Value
    '            contfee.Description = dtgListCont.Rows(i).Cells("Description").Value
    '            contfee.UnitPrice = dtgListCont.Rows(i).Cells("UnitPrice").Value
    '            contfee.Quantity = dtgListCont.Rows(i).Cells("Quantity").Value
    '            contfee.ID_Currency = dtgListCont.Rows(i).Cells("ID_Currency").Value

    '            db.SubmitChanges()
    '        Next

    '        btnEdit.Enabled = True
    '        btnSave.Enabled = False
    '        btncancel.Enabled = False
    '        dtgListCont.ReadOnly = True
    '        KhoiTaodtgContFee(m_ID_Cont)
    '    End Sub

    '    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
    '        btnSave.Enabled = True
    '        btncancel.Enabled = True
    '        btnEdit.Enabled = False
    '        dtgListCont.ReadOnly = False
    '    End Sub

    '    Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
    '        btnEdit.Enabled = True
    '        btnSave.Enabled = False
    '        btncancel.Enabled = False
    '        dtgListCont.ReadOnly = True
    '        KhoiTaodtgContFee(m_ID_Cont)
    '    End Sub

    '    Private Sub dtgContFee_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgListCont.CellValueChanged
    '        Try
    '            If e.ColumnIndex = 5 Or e.ColumnIndex = 4 Then
    '                Dim DonGia As Double = 0
    '                Dim SoLuong As Integer = 0
    '                DonGia = dtgListCont.CurrentRow.Cells("UnitPrice").Value
    '                SoLuong = dtgListCont.CurrentRow.Cells("Quantity").Value
    '                dtgListCont.CurrentRow.Cells("Price").Value = DonGia * SoLuong
    '            End If
    '        Catch ex As Exception

    '        End Try
    '    End Sub


    '    Private Sub AddComboBoxColumnsCurency()
    '        Dim comboboxColumn As DataGridViewComboBoxColumn
    '        comboboxColumn = CreateComboBoxColumnCurrency()
    '        SetAlternateChoicesUsingDataSourceCurrency(comboboxColumn)
    '        comboboxColumn.HeaderText = "Currency"
    '        dtgListCont.Columns.Add(comboboxColumn)

    '        'comboboxColumn = CreateComboBoxColumn()
    '        'SetAlternateChoicesUsingItems(comboboxColumn)
    '        'comboboxColumn.HeaderText = _
    '        '    "TitleOfCourtesy (via Items property)"
    '        '' Tack this example column onto the end.
    '        'DataGridView1.Columns.Add(comboboxColumn)
    '    End Sub

    '    Private Function CreateComboBoxColumnCurrency() As DataGridViewComboBoxColumn
    '        Dim column As New DataGridViewComboBoxColumn()

    '        With column
    '            .DataPropertyName = "ID_Currency"
    '            .HeaderText = "Currency"
    '            .DropDownWidth = 160
    '            .Width = 90
    '            .MaxDropDownItems = 3
    '            .FlatStyle = FlatStyle.Flat
    '            .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
    '        End With
    '        Return column
    '    End Function

    '    Private Sub SetAlternateChoicesUsingDataSourceCurrency(ByVal comboboxColumn As DataGridViewComboBoxColumn)
    '        Dim cur = From p In db.tblCurrencies
    '                       Select p
    '        With comboboxColumn
    '            .DataSource = cur
    '            .ValueMember = "ID_Currency"
    '            .DisplayMember = "Code"
    '        End With
    '    End Sub
#End Region



    'Private Sub dtgContFee_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    '    Try
    '        Dim ID_Cont As Integer = 0
    '        ID_Cont = dtgListCont.CurrentRow.Cells("ID_Cont").Value

    '        If uc Is Nothing Then
    '            uc = New ucTransportFee(ID_Cont)
    '            uc.Dock = DockStyle.Fill
    '            PanelEx3.Controls.Add(uc)
    '        Else
    '            uc.KhoiTaodtgContFee(ID_Cont)
    '        End If
    '    Catch ex As Exception

    '    End Try
    'End Sub

    Dim uc As ucTransportFee = Nothing

    Private Sub dtgListCont_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgListCont.DoubleClick
        Try
            Panel1.Visible = True
            PanelEx1.Visible = False
            BindingNavigator1.Visible = False
            Dim ID_Cont As Integer = 0
            ID_Cont = dtgListCont.CurrentRow.Cells("ID_Cont").Value
            Dim TieuDe As String = ""
            TieuDe = "File No: " + dtgListCont.CurrentRow.Cells("FileNo").Value.ToString + " - Cont No: " + dtgListCont.CurrentRow.Cells("ContNo").Value.ToString

            If uc Is Nothing Then
                uc = New ucTransportFee(ID_Cont, Me, TieuDe)
                uc.Dock = DockStyle.Fill
                Panel1.Controls.Add(uc)
            Else
                uc.ID_Cont = ID_Cont
                uc.KhoiTaodtgContFee(ID_Cont)
                uc.lblTieuDe.Text = TieuDe
            End If


        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Try
            Dim FromNo As Integer = 0
            Dim ToNo As Integer = 0
            Try
                FromNo = CInt(txtFrom.Text)
            Catch ex As Exception
                MsgBox("Số File không phải là số nguyên")
                txtFrom.Focus()
            End Try

            Try
                ToNo = CInt(txtTo.Text)
            Catch ex As Exception
                MsgBox("Số File không phải là số nguyên")
                txtTo.Focus()
            End Try

            If FromNo > ToNo Then
                MsgBox("Số File From phải nhỏ hơn To")
            End If

            KhoiTaodtgListCont(chkExport.Checked, FromNo, ToNo)
        Catch ex As Exception

        End Try
    End Sub
End Class
